package com.eclink.asset.asset.controller;

import cn.hutool.core.bean.BeanUtil;
import com.eclink.asset.convert.AssetItemConvert;
import com.eclink.asset.asset.domain.dao.AssetItem;
import com.eclink.asset.domain.AssetCategory;
import com.eclink.asset.pojo.query.AssetItemQuery;
import com.eclink.asset.pojo.vo.AssetItemVO;
import com.eclink.asset.asset.service.AssetItemService;
import com.eclink.asset.service.AssetCategoryService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 资产清单表Controller
 *
 * @author chenshixian
 * @date 2024-09-19
 */
@Api(tags ="资产清单表接口")
@RestController
@RequestMapping("/wms/assetItem")
public class AssetItemController extends BaseController {

    @Resource
    private AssetItemService service;

    @Resource
    private AssetCategoryService categoryService;

    @Resource
    private AssetItemConvert convert;

    @ApiOperation("查询")
    @PostMapping("/list")
    public ResponseEntity<Page<AssetItem>> list(@RequestBody AssetItemQuery query, Pageable page) {
        List<AssetItem> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("查询")
    @GetMapping("/listByIds/{ids}")
    public ResponseEntity<List<AssetItem>> listByIds(@PathVariable Long[] ids) {
        List<AssetItem> list = service.listByIds(ids);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("根据RFID标签查询")
    @GetMapping("/getAssetByRfid/{tag}")
    public ResponseEntity<List<AssetItem>> list(@PathVariable String tag) {
        List<AssetItem> list = service.selectByRfid(tag);
        return ResponseEntity.ok(list);
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public ResponseEntity<String> export(AssetItemQuery query) {
        List<AssetItem> list = service.selectList(query, null);
        List<AssetItemVO> voList = BeanUtil.copyToList(list, AssetItemVO.class);
        ExcelUtil<AssetItemVO> util = new ExcelUtil<>(AssetItemVO.class);
        return ResponseEntity.ok(util.writeExcel(voList, "资产清单表数据"));
    }

    @ApiOperation("导出资产导入模板")
    @GetMapping("/exportImportTemplate")
    public ResponseEntity<String> exportImportTemplate() {
        ExcelUtil<AssetItemVO> util = new ExcelUtil<>(AssetItemVO.class);
        Map<String, List<String>> dataConstraint = new HashMap<>();
        dataConstraint.put("categoryName", categoryService.selectListAll().stream()
                .map(AssetCategory::getName)
                .collect(Collectors.toList()));
        return ResponseEntity.ok(util.generateImportTemplate("资产清单表数据", dataConstraint));
    }

    @PostMapping("/importExcel")
    public ResponseEntity<Integer> importExcel(MultipartFile file) {
        try
        {
            InputStream is = file.getInputStream();
            ExcelUtil<AssetItemVO> util = new ExcelUtil<>(AssetItemVO.class);
            List<AssetItemVO> list = util.importExcel(is);

            for(AssetItemVO vo : list) {
                AssetItem item = convert.vo2do(vo);
                service.insert(item);
            }

            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file, MimeTypeUtils.isImg(file.getContentType()));
            AjaxResult ajax = AjaxResult.success();
            return ResponseEntity.ok(list.size());
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }


    @ApiOperation("获取资产清单表详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<AssetItem> getInfo(@PathVariable("id") Long id) {
        AssetItem res = service.selectById(id);

        return ResponseEntity.ok(res);
    }

    @ApiOperation("新增资产清单表")
    @Log(title = "资产清单表", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody AssetItem assetItem) {
        return ResponseEntity.ok(service.insert(assetItem));
    }

    @ApiOperation("修改资产清单表")
    @Log(title = "资产清单表", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody AssetItem assetItem) {
        return ResponseEntity.ok(service.update(assetItem));
    }


    @ApiOperation("删除资产清单表")
    @Log(title = "资产清单表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }


}
